﻿using DAL;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Model;
//引入序列号JSON命名空间
using System.Web.Script.Serialization;


namespace StudentManagerPro.Handlers
{
    /// <summary>
    /// JsonQuery 的摘要说明
    /// </summary>
    public class JsonQuery : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            //如果ContentType="text/plain"时前端接受到的json字符串，在前端需要将该字符串反序列化为json对象:
            //如var list = $.parseJSON(data);//将json字符串反序列化json对象
            //context.Response.ContentType = "text/plain";
            //ContentType = "application/json"时返回给前端的就是json对象，能够直接解析
            context.Response.ContentType = "application/json";
            //获取班级名称
            var className = context.Request.Params["className"].ToString();
            var scoreList = new List<StudentExt>
            {
                new StudentExt{ StudentId=1000, StudentName="龚均强" ,ClassName=className},
                new StudentExt{ StudentId=1001, StudentName="龚均强" ,ClassName=className},
                new StudentExt{ StudentId=1002, StudentName="龚均强" ,ClassName=className},
                new StudentExt{ StudentId=1003, StudentName="龚均强" ,ClassName=className}
            };
            //将集合序列号为json字符串
            //方式一：
            //JavaScriptSerializer jss = new JavaScriptSerializer();
            //var strlist = jss.Serialize(scoreList);
            //方式二：
            var strlist = JsonConvert.SerializeObject(scoreList);
            //返回结果
            context.Response.Write(strlist);
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}